<template>
  <el-dialog
    width="500px"
    title="员工导入"
    :visible.sync="showExcelDialog"
  >
    <el-row type="flex" justify="center">
      <div class="upload-excel">
        <input
          ref="excel-upload-input"
          class="excel-upload-input"
          type="file"
          accept=".xlsx, .xls"
          @change="UploadExcel"
        >
        <div class="drop">
          <i class="el-icon-upload" />
          <el-button type="text" @click="ImportEmployee">下载导入模板</el-button>
          <span>将文件拖到此处或
            <el-button type="text" @click="handleUpload">点击上传</el-button>
          </span>
        </div>
      </div>
    </el-row>
    <el-row type="flex" justify="end">
      <el-button size="mini" type="primary" @click="confirmUpload">确定上传</el-button>
      <el-button size="mini" @click="showExcelDialog = false">取消</el-button>
    </el-row>
  </el-dialog>
</template>
<script>
import { importEmployee, uploadExcel } from '@/api/employee.js'
import FileSaver from 'file-saver'
export default {
  name: 'Importexcel',
  data() {
    return {
      file: null,
      showExcelDialog: false
    }
  },
  methods: {
    // 下载导入模板
    async ImportEmployee() {
      const res = await importEmployee()
      FileSaver.saveAs(res, '员工导入模版.xlsx') // 下载文件
    },
    // 弹出文件选择器 input file类型组件 只接受excel文件
    handleUpload() {
      this.$refs['excel-upload-input'].click() // 调出input文件选择器
    },
    // 当文件传输上去后的监听事件
    UploadExcel(event) {
      console.log(event)
      this.$message.success({ message: '文件上传成功', duration: 1500 })
      // 需要判断文件列表长度是否为0
      this.file = event.target.files // file是一个对象
      console.log(this.file)
      console.log(this.file.length)
    },
    // 确定上传
    async confirmUpload() {
      console.log(11)
      if (this.file.length > 0) {
      // 文件长度大于0 调用接口
        const data = new FormData() // form-data类型
        data.append('file', this.file[0]) // form-data加参数 参数名file 值file[0]
        try {
          await uploadExcel(data)
          this.$emit('uploadSuccess') // 导入员工成功 通知父组件更新
          this.$message.success('员工导入成功')
          this.showExcelDialog = false // 关闭弹层
        } catch (error) {
        // 捕获失败
          new Error(error)
        } finally {
          // 无论是执行try还是catch 都会走finally
          this.$refs['excel-upload-input'].value = '' // 空值
        }
      }
    }
  }

}
</script>

<style scoped lang="scss">
    .upload-excel {
      display: flex;
      justify-content: center;
      margin: 20px;
      width: 360px;
      height: 180px;
      align-items: center;
      color: #697086;
      .excel-upload-input {
        display: none;
        z-index: -9999;
      }
      .btn-upload,
      .drop {
        border: 1px dashed #dcdfe6;
        width: 100%;
        height: 100%;
        text-align: center;
        line-height: 160px;
        border-radius: 8px;
        display: flex;
        flex-direction: column;
        justify-content: center;
      }
      .drop {
        line-height: 40px;
        color: #bbb;
        i {
          font-size: 60px;
          display: block;
          color: #c0c4cc;
        }
      }
    }
</style>
